package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jpa.interfaces.KlantBeanInterface;
import jpa.interfaces.WinkelmandBeanInterface;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		if (isLoggedIn(request.getSession())) {
			forwardTo("WEB-INF/welcome.jsp", request, response);
		} 
		else {
			PrintWriter out = response.getWriter();
			out.println("<html><head></head><body><center><br /><form action=\"LoginServlet\" method=\"POST\">"
					+ "<table><tr><td>Gebruikersnaam</td>"
					+ "<td><input type=\"text\" name=\"username\"><td></tr>"
					+ "</table>"
					+ "<input type=\"submit\" value=\"login\"></form>" 
					+"<a href=\"RegisterServlet\">Aanmelden als nieuwe klant</a></center></body><html>");
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String username = "";
		username = request.getParameter("username");
			

		System.out.println("usernamen: " + username);
		InitialContext context;
		
		try {
			context = new InitialContext();
			KlantBeanInterface kbin = (KlantBeanInterface) context.lookup("KlantBeans");
			WinkelmandBeanInterface wwagen = (WinkelmandBeanInterface) context.lookup("WinkelmandBeans");
			wwagen.setKlantId(kbin.getKlantId(username));
			
			if (kbin.login(username) == true) {
				request.getSession().setAttribute("loggedIn", true);
				request.getSession().setAttribute("welcome", username);
				request.getSession().setAttribute("shoppingCar", wwagen);
				forwardTo("WEB-INF/welcome.jsp", request, response);
			} else {
				response.sendRedirect(response
						.encodeRedirectURL("LoginServlet"));
			}
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	private void forwardTo(String jsp, HttpServletRequest request,
			HttpServletResponse response) {
		RequestDispatcher rd = request.getRequestDispatcher(jsp);
		try {
			rd.forward(request, response);
		} catch (Exception e) {

		}
	}

	private boolean isLoggedIn(HttpSession session) {
		Object loggedIn = session.getAttribute("loggedIn");
		boolean isLoggedIn = false;
		if (loggedIn instanceof Boolean) {
			isLoggedIn = (Boolean) loggedIn;
		}
		return isLoggedIn;
	}

}
